package com.luoqiz.project.screenBroadcard;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.luoqiz.project.screenBroadcard.client.ScreenDataInfo;
import com.luoqiz.utils.DataConvertUtils;

public class ScreenOper {

	/**
	 * 组装包数据，还原为图片
	 * @param len 
	 * 
	 * @return
	 */
	public ScreenDataInfo recr(byte[] screenData, int len) {
		ScreenDataInfo sdi = new ScreenDataInfo();
		// 解析时间戳
		sdi.setTiemstamp(DataConvertUtils.byteArrayToLong(screenData, 0));
		// 解析包数量
		sdi.setPackageCount(DataConvertUtils.byteArrayToInt(screenData, 8));
		sdi.setPackageIndex(DataConvertUtils.byteArrayToInt(screenData, 12));
		int contentLen = len - 16;
		byte[] content = new byte[contentLen];
		System.arraycopy(screenData, 16, content, 0, contentLen);
		sdi.setContent(content);
		return sdi;
	}

	/**
	 * 抓取的图片大小超过64k，将其拆分为小包返回
	 */
	public List<byte[]> getListPackage(byte[] screenData) {
		// 每个包大小
		int packageSize = 60 * 1024;
		// 拆分包的个数
		int screenLen = screenData.length;
		int packgeCount = 0;
		if (screenLen % packageSize == 0) {
			packgeCount = screenLen / packageSize;
		} else {
			packgeCount = screenLen / packageSize + 1;
		}

		// 设计每个包的长度,组成方式：8字节（同一屏的时间戳）+4字节（同一屏包的个数）+4字节（当前包属于第几个）+60*1024字节（数据）
		// 时间戳和包数量字节数组
		byte[] buf = new byte[12];
		byte[] timestamp = getTimestamp();
		System.arraycopy(timestamp, 0, buf, 0, 8);
		System.arraycopy(DataConvertUtils.intToByteArray(packgeCount), 0, buf, 8, 4);
		List<byte[]> arrayList = new ArrayList<byte[]>();
		// 构造数据包
		for (int i = 0; i < packgeCount; i++) {
			// System.out.println("时间："+DataConvertUtils.byteArrayToLong(timestamp,
			// 0)+"---包索引："+i);
			if (i != packgeCount - 1) {
				//放入时间戳和包数量字节数组
				byte[] pack = new byte[16 + packageSize];
				System.arraycopy(buf, 0, pack, 0, 12);
				System.arraycopy(DataConvertUtils.intToByteArray(i), 0, pack, 12, 4);
				System.arraycopy(screenData, i * packageSize, pack, 16, packageSize);
				arrayList.add(pack);
			} else {
				int laseDataLen = screenLen % packageSize;
				byte[] lastPpack = new byte[16 + laseDataLen];
				System.arraycopy(buf, 0, lastPpack, 0, 12);
				System.arraycopy(DataConvertUtils.intToByteArray(i), 0, lastPpack, 12, 4);
				System.arraycopy(screenData, i * packageSize, lastPpack, 16, laseDataLen);
				arrayList.add(lastPpack);
			}
		}
		return arrayList;
	}

	private byte[] getTimestamp() {
		long l = System.currentTimeMillis();
		return DataConvertUtils.longToByteArray(l);
	}

	/**
	 * 保存图片
	 * 
	 * @param
	 */
	public void saveScreen(String filepath) {
		BufferedImage b = getScreen();
		File f = new File(filepath);
		try {
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
			ImageIO.write(b, "jpg", bos);
			bos.flush();
			bos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送指定图片
	 */
	public BufferedImage getPic() {
		BufferedImage b = null;
		try {
			b = ImageIO.read(new File("d:/dome/img12.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return b;
	}

	/**
	 * 抓图
	 */
	public BufferedImage getScreen() {
		Robot robot = null;
		try {
			robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
		Rectangle screenRect = new Rectangle(0, 0, 1366, 768);
		return robot.createScreenCapture(screenRect);
	}
}
